package com.lhl.fw.core.event;

import lombok.Getter;
import org.springframework.context.ApplicationEvent;

/**
 * @className: com.lhl.fw.core.event.CustomSpringEvent
 * @description: TODO 类描述
 * @author: king
 * @date: 2020-11-06
 **/
@Getter
public class CustomSpringEvent<T> extends ApplicationEvent {
    private static final long serialVersionUID = 1L;
    //数据
    private T t;
    //事件标识,一般添加前缀如"sys.menu.add"
    private String actionType;
    public CustomSpringEvent(Object source,T t,String actionType) {
        super(source);
        this.t = t;
        this.actionType = actionType;
    }
}
/****************使用方式如下**************************/
/**************************使用,事件发送*******************************************************/
//使用,事件发送：2：发送（可以不需要发送对象，直接使用ApplicationEventPublisher ）
/*@Component
public class CustomSpringEventPublisher {
    @Autowired
    private ApplicationEventPublisher applicationEventPublisher;

    public void doStuffAndPublishAnEvent(final String message) {
        System.out.println("Publishing custom event. ");
        CustomSpringEvent<String> customSpringEvent = new CustomSpringEvent<>(this, message,"add");
        applicationEventPublisher.publishEvent(customSpringEvent);
    }
}*/
/*****************************监听对象*******************************************/
/*@Component
public class CustomSpringEventListener {

    @EventListener(condition = "#event.getActiontype()=='add'")
    public void optT(CustomSpringEvent<String> event) {
        System.out.println("Received spring custom event - " + event.getT());
    }

    @EventListener(condition = "#event.getActiontype()=='update'")
    public void optUpdate(CustomSpringEvent<String> event) {
        System.out.println("Received spring custom event - " + event.getT());
    }
}*/